<template>
  <div class="add">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert title="消息提示的文案" type="info" center show-icon></el-alert>
      <el-steps :space="200" :active="steping-0" align-center finish-status="success">
        <el-step title="商品信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
      </el-steps>
      <el-form
        :model="addForm"
        status-icon
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
        style="margin-top:15px;"
        label-position="top"
      >
        <!-- <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>-->
        <el-tabs
          :tab-position="'left'"
          v-model="steping"
          @tab-click="handleClick"
          :before-leave="beforeLeave"
        >
          <el-tab-pane label="商品信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="addForm.goods_price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="addForm.goods_weight" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="addForm.goods_number" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="goodsCate">
              <el-cascader
                v-model="addForm.goodsCate"
                :options="getGoodsCate"
                :props="cateProps"
                @change="handleChange"
                expandTrigger="hover"
                style="width:25%"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <el-form-item :label="item.attr_name" v-for="item in this.manyList" :key="item.attr_id">
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox border :label="el" v-for="(el,index ) in item.attr_vals" :key="index"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
            <el-form-item
              :label="item.attr_name"
              v-for="item in this.addForm.onlyList"
              :key="item.attr_id"
            >
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <el-upload
              class="upload-demo"
              action="http://127.0.0.1:8888/api/private/v1/upload"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :headers="header"
              :on-success="handleSuccess"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <quill-editor v-model="addForm.content"></quill-editor>
            <el-row>
              <el-col>
                <el-button
                  size="small"
                  style="margin-top:15px;"
                  type="primary"
                  @click="submitAddForm"
                >商品添加</el-button>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
    <el-dialog title="图片预览" :visible.sync="imgViews" width="50%">
      <img :src="PreviewImg" alt="xxx" style="width:100%" />
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'add',
  data() {
    return {
      addForm: {
        goods_name: '',
        goods_price: '0',
        goods_weight: '0',
        goods_number: '0',
        goodsCate: [],
        onlyList: [],
        goodsCate1: '',
        pics: [],
        content: '',
        attrs: []
      },
      manyList: [],
      addFormRules: {
        goods_name: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            trigger: 'blur'
          }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],

        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goodsCate: [
          { required: true, message: '请输入商品分类', trigger: 'blur' }
        ]
      },
      steping: '0',
      getGoodsCate: [],

      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      header: {
        Authorization: ''
      },
      PreviewImg: '',
      imgViews: false
    }
  },
  created() {
    this.getCateList()

    this.header.Authorization = window.sessionStorage.getItem('token')
  },

  methods: {
    handleClick(e) {
      this.steping = e.name
      //   console.log(this.steping)
      if (this.steping == 1 && this.addForm.goodsCate.length == 3) {
        this.$axios
          .get(`categories/${this.addForm.goodsCate[2]}/attributes`, {
            params: { sel: 'many' }
          })
          .then(result => {
            // console.log(result)
            result.data.data.forEach(item => {
              if (item.attr_vals == '') {
                item.attr_vals = []
              } else {
                item.attr_vals = item.attr_vals.split(' ')
              }
            })
            this.manyList = result.data.data
            console.log(this.manyList)
          })
      } else if (this.steping == 2 && this.addForm.goodsCate.length == 3) {
        this.$axios
          .get(`categories/${this.addForm.goodsCate[2]}/attributes`, {
            params: { sel: 'only' }
          })
          .then(result => {
            this.addForm.onlyList = result.data.data
            // console.log(this.addForm.onlyList)
          })
      }
    },
    getCateList() {
      this.$axios.get('categories').then(result => {
        // console.log(result, 1)
        this.getGoodsCate = result.data.data
      })
    },
    handleChange() {
      console.log(this.addForm.goodsCate)
      if (this.addForm.goodsCate.length != 3) {
        this.addForm.goodsCate = []
      }
    },
    beforeLeave(active, old) {
      console.log('离开' + old, '进入' + active)
      if (this.addForm.goodsCate.length != 3) {
        this.$message({
          type: 'error',
          message: '请选择分类'
        })
        return false
      }
    },
    handlePreview(file) {
      this.PreviewImg = file.response.data.url
      this.imgViews = true
    },
    handleRemove(file) {
      console.log(file)
      const imgDelete = file.response.data.tmp_path
      const i = this.addForm.pics.findIndex(x => x.pic === imgDelete)
      // console.log(i)
      this.addForm.pics.splice(i, 1)
      console.log(this.addForm.pics)
    },
    handleSuccess(res) {
      const imgObj = { pic: res.data.tmp_path }
      this.addForm.pics.push(imgObj)
      console.log(this.addForm.pics)
    },
    submitAddForm() {
      this.$refs.addFormRef.validate(valid => {
        if (valid) {
          // alert('submit!')
          this.addForm.goodsCate1 = this.addForm.goodsCate.join(',')
          // console.log(this.addForm.onlyList)
          // console.log(this.manyList)
          this.manyList.forEach(item => {
            const newOnly = {
              attr_id: item.attr_id,
              attr_value: item.attr_vals.join(' ')
            }
            this.addForm.attrs.push(newOnly)
          })
          this.addForm.onlyList.forEach(item => {
            const newOnly = {
              attr_id: item.attr_id,
              attr_value: item.attr_vals
            }
            this.addForm.attrs.push(newOnly)
          })
          console.log(this.addForm)
          this.$axios
            .post('goods', {
              goods_name: this.addForm.goods_name,
              goods_cat: this.addForm.goodsCate1,
              goods_price: this.addForm.goods_price,
              goods_number: this.addForm.goods_number,
              goods_weight: this.addForm.goods_weight,
              goods_introduce: this.addForm.content,
              attrs: this.addForm.attrs,
              pics: this.addForm.pics
            })
            .then(result => {
              console.log(result)
              if (result.data.meta.status == 200) {
                this.$message({
                  type: 'success',
                  message: result.data.meta.msg
                })
              } else {
                this.$message({
                  type: 'error',
                  message: result.data.meta.msg
                })
              }
              this.$router.push({ name: 'GoodsList' })
            })
        } else {
          this.$message({
            type: 'error',
            message: '请填写必要的表单项'
          })
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.el-checkbox {
  margin-right: 0;
}
</style>